package visualInterface;

import java.awt.AlphaComposite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class BackgroungImages extends JPanel {
	BufferedImage image = null;
	static int width;
	static int height;
	JFrame frame;
	

	public BackgroungImages(String imagename, JFrame window) {
		frame = window;
		if (imagename != null) {
		try {
				image = ImageIO.read(new File(imagename));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		if (image != null) {
			int type = image.getType() == 0 ? BufferedImage.TYPE_INT_ARGB
					: image.getType();

			width = frame.getWidth();
			height = frame.getHeight();
			
			BufferedImage resizeImageJpg = resizeImageWithHint(image, type);
			
			g.drawImage(resizeImageJpg, 0, 0, null);
		}
	}

	private  BufferedImage resizeImageWithHint(
			BufferedImage originalImage, int type) {

		BufferedImage resizedImage = new BufferedImage(width, height, type);
		Graphics2D g = resizedImage.createGraphics();
		g.drawImage(originalImage, 0, 0, width, height, null);
		g.dispose();
		g.setComposite(AlphaComposite.Src);

		g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
				RenderingHints.VALUE_INTERPOLATION_BILINEAR);
		g.setRenderingHint(RenderingHints.KEY_RENDERING,
				RenderingHints.VALUE_RENDER_QUALITY);
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		return resizedImage;
	}
}
